package RealGenotype;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import Generic.*;

public class RealGenotype implements Genotype{

	private List<Double> parameters;
	private double standardDeviation;
	private Random generator;

	public RealGenotype(int numberOfParameters,double standardDeviation){
		generator = new Random();
		this.standardDeviation = standardDeviation;
		parameters = new ArrayList<Double>();
		for(int i = 0; i<numberOfParameters; i++){
			parameters.add(0.0);
		}
	}

	public void randomize() {
		for(int i = 0; i<parameters.size(); i++)
		{
			parameters.set(i,Math.random());
		}
	}


	public Genotype copy() {
		RealGenotype copy=new RealGenotype(parameters.size(),standardDeviation);
		for(int i = 0; i<parameters.size(); i++)
		{
			copy.parameters.set(i,parameters.get(i));
		}
		return copy;
	}

	public void mutate(double mutationRate) {
		for(int i = 0; i<parameters.size(); i++)
		{
			if(Math.random()<mutationRate)
			{
				double newValue = parameters.get(i)+generator.nextGaussian()*standardDeviation;
				newValue = newValue<0.0?0.0:(newValue>1.0?1.0:newValue);
				parameters.set(i, newValue);
			}
		}
	}

	public double get(int index)
	{
		return parameters.get(index);
	}

	public void onePointCrossover(Genotype gtype) {
		RealGenotype rgType = (RealGenotype)gtype;

		for(int i = 0; i < parameters.size()*Math.random() ;i++)
		{
			double temp = rgType.parameters.get(i);
			rgType.parameters.set(i,parameters.get(i));
			parameters.set(i,temp);
		}
	}

	public void uniformCrossover(Genotype gtype) {
		RealGenotype rgType = (RealGenotype)gtype;

		for(int i = 0; i < parameters.size() ;i++)
		{
			if(Math.random()<.5)
			{
				double temp = rgType.parameters.get(i);
				rgType.parameters.set(i,parameters.get(i));
				parameters.set(i,temp);
			}
		}
	}
}
